package com.PMSys.Servlet;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.PMSys.Demain.DepartmentalMob;
import com.PMSys.Demain.Dimission;
import com.PMSys.Demain.PostMob;
import com.PMSys.Demain.Staff;
import com.PMSys.Service.ReportManagementService;

/**
 * Servlet implementation class ReportManagementServlet
 */
public class ReportManagementServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private ReportManagementService reportManagementService;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReportManagementServlet() {
		super();
		
	}

	@Override
	public void init() throws ServletException {
		reportManagementService = new ReportManagementService();
	}
	
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=UTF-8");

		String method = request.getParameter("method");
		
		String PostMobAll = request.getParameter("PostMobAll");
		
		String DimissionAll = request.getParameter("DimissionAll");
		
		String PersonnelAll = request.getParameter("PersonnelAll");
		
		String StaffAll = request.getParameter("StaffAll");
		
		String DepartmentMobAll = request.getParameter("DepartmentMobAll");
		

		if ("newEmployess".equals(method)) {
			newEmployess(request, response);
		}

		if ("departmentalMob".equals(method)) {
			departmentalMob(request, response);
		}
		
		if(PostMobAll!=null && PostMobAll!=""){
			depostMob(request, response);
		}
		
		if(StaffAll!=null && StaffAll!=""){
			newEmployess(request, response);
		}
		
		if(DimissionAll!=null && DimissionAll!=""){
			dimissionMob(request, response);
		}
		
		if(PersonnelAll!=null && PersonnelAll!=""){
			personalMob(request, response);
		}
		
		if(DepartmentMobAll!=null && DepartmentMobAll!=""){
			departmentalMob(request, response);
		}
		
		
	}
	



	
	private void depostMob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  {

		String startDateString = request.getParameter("startDate");
		String endDateString = request.getParameter("endDate");
		
		Date startDate = null;
		Date endDate = null;

		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (startDateString != null && !"".equals(startDateString)) {
				startDate = format.parse(startDateString);
			} else {
				startDate = getMonthFirstDay();

			}
			if (endDateString != null && !"".equals(endDateString)) {
				endDate = format.parse(endDateString);
			} else {
				endDate = getMonthEndDay();
			}

		} catch (ParseException e) {
			e.printStackTrace();
			response.getWriter().print("<script>alert('查询失败');window.history.back();</script>");
		}
		

		List<PostMob> postList = reportManagementService.selectPostMob(startDate, endDate);

		if (postList != null && postList.size() != 0) {
			
			request.setAttribute("postList", postList);
			request.getRequestDispatcher("/ReportManagement/PostMobAll.jsp").forward(request, response);
		} 
		else {
			response.getWriter().print("<script>alert('当前时间内无查询结果');location.href=" + 
		           "'"+request.getContextPath()+"/ReportManagement/PostMobAll.jsp'</script>");
			
		}
		
	}
	
	
	
	
	private void dimissionMob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	
		String startDateString = request.getParameter("startDate");
		String endDateString = request.getParameter("endDate");
		
		Date startDate = null;
		Date endDate = null;

		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (startDateString != null && !"".equals(startDateString)) {
				startDate = format.parse(startDateString);
			} else {
				startDate = getMonthFirstDay();

			}
			if (endDateString != null && !"".equals(endDateString)) {
				endDate = format.parse(endDateString);
			} else {
				endDate = getMonthEndDay();
			}

		} catch (ParseException e) {
			e.printStackTrace();
			response.getWriter().print("<script>alert('查询失败');window.history.back();</script>");
		}
		

		List<Dimission> dimissionList = reportManagementService.selectDimission(startDate, endDate);

		if (dimissionList != null && dimissionList.size() != 0) {
			
			request.setAttribute("dimissionList", dimissionList);
			request.getRequestDispatcher("/ReportManagement/DimissionAll.jsp").forward(request, response);
		} 
		else {
			response.getWriter().print("<script>alert('当前时间内无查询结果');location.href=" + 
		           "'"+request.getContextPath()+"/ReportManagement/DimissionAll.jsp'</script>");
			
		}
	}
	
	private void personalMob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().print("<script>alert('当前时间内无查询结果');location.href=" + 
		           "'"+request.getContextPath()+"/ReportManagement/PersonnelAll.jsp'</script>");
		
	}
	
	
	
	private void newEmployess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		String startDateString = request.getParameter("startDate");
		String endDateString = request.getParameter("endDate");

		Date startDate = null;
		Date endDate = null;

		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (startDateString != null && !"".equals(startDateString)) {
				startDate = format.parse(startDateString);
			} else {
				startDate = getMonthFirstDay();

			}
			if (endDateString != null && !"".equals(endDateString)) {
				endDate = format.parse(endDateString);
			} else {
				endDate = getMonthEndDay();
			}

		} catch (ParseException e) {
			e.printStackTrace();
			response.getWriter().print("<script>alert('查询失败');window.history.back();</script>");
		}

		List<Staff> newEmployesList = reportManagementService.selectNewEmployess(startDate, endDate);

		if (newEmployesList != null && newEmployesList.size() != 0) {
			request.setAttribute("newEmployesList", newEmployesList);
			request.getRequestDispatcher("/ReportManagement/newEmployess.jsp").forward(request, response);
		} else {
			
			response.getWriter().print("<script>alert('当前时间内无查询结果');location.href=" + 
			           "'"+request.getContextPath()+"/ReportManagement/newEmployess.jsp'</script>");
		}

	}

	private void departmentalMob(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String startDateString = request.getParameter("startDate");
		String endDateString = request.getParameter("endDate");

		Date startDate = null;
		Date endDate = null;

		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if (startDateString != null && !"".equals(startDateString)) {
				startDate = format.parse(startDateString);
			} else {
				startDate = getMonthFirstDay();

			}
			if (endDateString != null && !"".equals(endDateString)) {
				endDate = format.parse(endDateString);
			} else {
				endDate = getMonthEndDay();
			}

		} catch (ParseException e) {
			e.printStackTrace();
			response.getWriter().print("<script>alert('查询失败');window.history.back();</script>");
		}

		List<DepartmentalMob> departmentalMobList = reportManagementService.selectDepartmentalMob(startDate, endDate);

		if (departmentalMobList != null && departmentalMobList.size() != 0) {
			request.setAttribute("departmentalMobList", departmentalMobList);
			request.getRequestDispatcher("/ReportManagement/departmentalMob.jsp").forward(request, response);
		} else {
			
			response.getWriter().print("<script>alert('当前时间内无查询结果');location.href=" + 
			           "'"+request.getContextPath()+"/ReportManagement/departmentalMob.jsp'</script>");
		}
	}

	/**
	 * 获取当前月第一天
	 * 
	 * @return
	 */
	private Date getMonthFirstDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}

	/**
	 * 获取当前月最后一天
	 * 
	 * @return
	 */
	private Date getMonthEndDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
		return calendar.getTime();
	}
}
